import {Component, OnInit} from '@angular/core';
import {WsService} from "../../../core/net/ws.service";
import {FormBuilder, FormGroup} from "@angular/forms";
import {FetchService} from "../../../core/net/fetch.service";
import {Router} from "@angular/router";
import {NzMessageService} from "ng-zorro-antd";
import {isToday, format} from 'date-fns';

@Component({
  selector: 'app-msg-center',
  templateUrl: './msg-center-zx.component.html',
  styleUrls: ['./msg-center-zx.component.scss']
})
export class MsgCenterZxComponent implements OnInit {
  msg = '';
  searchForm: FormGroup;
  manageId = JSON.parse(localStorage.userInfo).manageId;
  tableData = [];
  total: number;
  pageNum = 1;
  pageSize = 15;
  loading = false;
  detailForm: FormGroup;
  detailModalShow: boolean = false;
  nzBodyStyle: any = {
    padding: '10px'
  };

  xxzfForm: FormGroup;
  xxzfModalShow: boolean = false;
  selectedType;
  msgTypeList = [
    {label: '全部消息', value: ''},
    {label: '调度消息', value: '1'},
    {label: '警情消息', value: '2'},
    {label: '计划消息', value: '3'},
    {label: '报送消息', value: '4'},
  ];
  rowId = '';

  constructor(private fb: FormBuilder,
              private fetchService: FetchService,
              private router: Router,
              private wsService: WsService,
              private message: NzMessageService) {
    this.searchForm = this.fb.group({
      content: [null],
      sendDate: [null],
      fromMember: [null],
      toMember: [null],
      state: ['']
    });

    this.detailForm = this.fb.group({
      id: [null],
      sendManageName: ['唐徕渠管理处'],
      acceptManageName: ['调度中心'],
      sendTime: ['2020-06-05 13:45'],
      content: ['唐徕渠申请加水5方']
    });

    this.xxzfForm = this.fb.group({
      id: [null],
      sendManageName: ['唐徕渠管理处'],
      acceptManageName: ['调度中心'],
      content: ['唐徕渠申请加水5方']
    });

    this.wsService.messageSubject.subscribe(
      data => {
        console.log(data);
        this.queryInfo();
      },
      err => console.log(err)
    );
  }

  ngOnInit(): void {
    this.selectMsgType('');
  }

  queryInfo(reset: boolean = false): void {
    this.loading = true;
    this.tableData = [];
    if (reset) {
      this.pageNum = 1;
    }
    const param = {
      firstType: this.selectedType,
      content: this.searchForm.value.content,
      sendDate: this.searchForm.value.sendDate == null ? null : format(this.searchForm.value.sendDate, 'YYYY-MM-DD'),
      state: this.searchForm.value.state,
      pageNum: this.pageNum,
      pageSize: this.pageSize,
      orderByField: "state, sendTime",
      orderBy: "desc "
    };
    this.fetchService.post<any>('sys/message/list', param).subscribe(res => {
      this.tableData = res.list;
      this.total = res.total;
      this.loading = false;
    });
  }

  // 选择消息类型
  selectMsgType(data): void {
    this.selectedType = data;
    this.queryInfo();
  }

  onCancel() {
    this.detailModalShow = false;
  }

  goPage(row, url): void {
    this.rowId = row.id;
    if (row.state == '0') {
      this.readMsg();
    }
    this.router.navigate([url]);
  }

  readMsg(): void {
    this.fetchService.get(`sys/message/read/${this.rowId}`).subscribe(res => {
    });
  }

  view(data): void {
    this.rowId = data.id;
    this.detailForm.patchValue(data);
    this.detailModalShow = true;
    if (data.state == '0' && data.needForward == '1') {
      // 未读并且可以转发的时候，不调read接口
    } else {
      this.readMsg();
    }
  }

  detailModalClose(): void {
    this.detailModalShow = false;
  }

  openXxzfModal(row): void {
    this.rowId = row.id;
    this.getZfMsgDetail(row.id);
    this.xxzfModalShow = true;
  }

  getZfMsgDetail(id): void {
    this.fetchService.get<any>(`sys/message/${id}`).subscribe(res => {
      this.xxzfForm.patchValue(res);
    });
  }

  xxzfConfirm(): void {
    const param = {
      id: this.rowId,
      content: this.xxzfForm.value.content
    };
    this.fetchService.post<any>('sys/message/redirect', param).subscribe(res => {
      this.message.success('操作成功');
      this.xxzfModalShow = false;
    });
  }
}
